package com.example.administrator.mymusic;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * Created by Administrator on 2013/1/2.
 */
public class XiaFragment extends Fragment implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    private SharedPreferences sharedPreferences;
    private MyService myService;
    private MainActivity mainActivity;
    private ImageView imStart, mXunHuan;
    private ObjectAnimator oAnimator;
    public SeekBar songsSb;
    public TextView songsTv;

    public ObjectAnimator getoAnimator() {
        return oAnimator;
    }

    private float songIvStart = 0;
    private LinearLayout songsDonghua;

    public ImageView getImStart() {
        return imStart;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_xia, null);
        mainActivity = (MainActivity) getActivity();
        imStart = (ImageView) view.findViewById(R.id.songs_start);
        songsDonghua = (LinearLayout) view.findViewById(R.id.songs_donghua);
        songsDonghua.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        songsSb = (SeekBar) view.findViewById(R.id.songs_sb);
        songsTv = (TextView) view.findViewById(R.id.songs_tv);
        mXunHuan = (ImageView) view.findViewById(R.id.songs_shunxu);
        mXunHuan.setOnClickListener(this);
        imStart.setOnClickListener(this);
        view.findViewById(R.id.songs_go).setOnClickListener(this);
        view.findViewById(R.id.songs_back).setOnClickListener(this);
        songsSb.setOnSeekBarChangeListener(this);
        XuanZhuan(0);


        sharedPreferences = getActivity().getSharedPreferences("app", Context.MODE_PRIVATE);
        MainActivity.sXunhuan = sharedPreferences.getInt("sXunhuan", 0);
//        songsSb.setProgress(sharedPreferences.getInt("songsSb",0));
        Log.d("qwert", "onCreateView: " + MainActivity.sXunhuan);
        switch (MainActivity.sXunhuan) {
            case 0:
                mXunHuan.setImageResource(MainActivity.sXunHuanIM[0]);
                break;
            case 1:
                mXunHuan.setImageResource(MainActivity.sXunHuanIM[1]);
                break;
            case 2:
                mXunHuan.setImageResource(MainActivity.sXunHuanIM[2]);
                break;
        }
        return view;
    }


    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        myService = mainActivity.getMyService();
        switch (v.getId()) {
            case R.id.songs_start:
                myService.start_pause();
                if (!myService.isRuning) {
                    imStart.setBackgroundResource(R.mipmap.begin);
                } else {
                    imStart.setBackgroundResource(R.mipmap.pause);
                }
                if (oAnimator.isPaused()) {
                    XuanZhuan(songIvStart);
                    oAnimator.start();
                } else {
                    oAnimator.pause();
                }
                if (!oAnimator.isStarted()) {
                    XuanZhuan(0);
                    oAnimator.start();
                }
                break;
            case R.id.songs_go:
                oAnimator.pause();
                XuanZhuan(0);
                oAnimator.start();
                myService.go();
                break;
            case R.id.songs_back:
                oAnimator.pause();
                XuanZhuan(0);
                oAnimator.start();
                myService.back();
                break;
            case R.id.songs_shunxu:
                MainActivity.sXunhuan++;
                switch (MainActivity.sXunhuan % 3) {
                    case 0:
                        mXunHuan.setImageResource(MainActivity.sXunHuanIM[0]);
                        break;
                    case 1:
                        mXunHuan.setImageResource(MainActivity.sXunHuanIM[1]);
                        break;
                    case 2:
                        mXunHuan.setImageResource(MainActivity.sXunHuanIM[2]);
                        break;
                }
        }
    }


    public void XuanZhuan(float Start) {
        oAnimator = ObjectAnimator.ofFloat(songsDonghua, "rotation", Start, Start + 360);
        oAnimator.setDuration(10000);
        oAnimator.setRepeatCount(-1);
        LinearInterpolator lin = new LinearInterpolator();
        oAnimator.setInterpolator(lin);
        oAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                songIvStart = (float) animation.getAnimatedValue();
            }
        });
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    private boolean isTouch = false;

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        isTouch = true;
    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        myService = mainActivity.getMyService();
        isTouch = false;
        myService.Seekbar(isTouch, songsSb.getProgress());
//        LrcFragment lrcFragment= (LrcFragment) mainActivity.getZhongFragment().getFragmentList().get(1);
//        lrcFragment.getListView().setSelection(lrcFragment.getIndex(songsSb.getProgress()));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("qqqqqqq", "onDestroy: ");
//        sharedPreferences.edit().putInt("songsSb", songsSb.getProgress());
        sharedPreferences.edit().putInt("sXunhuan", MainActivity.sXunhuan % 3).commit();

    }

}
